package org.dromara.common.core.utils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MarkdownUtil {

    // 生成标题
    public static String createHeaderN(int level, String text) {
        return "#".repeat(Math.max(0, level)) + " " + text + "\n";
    }

    public static String createHeader(int level, String text) {
        return "#".repeat(Math.max(0, level)) + " " + text;
    }

    // 加粗
    public static String createBold(String text) {
        return "**" + text.trim() + "**";
    }

    // 代码
    public static String createCode(String text) {
        return "`" + text.trim() + "`";
    }

    // 引用
    public static String createQuote(String text) {
        return "> " + text.trim() + "\n";
    }

    // 生成字体颜色 info 绿色, warning 橙红色, comment 灰色
    public static String createFont(String text, String color) {
        return "<font color=\\\""+color+"\\\">" + text.trim() + "</font>";
    }

    public static String createPriority(String priority) {
        String color = switch (priority) {
            case "高" -> "warning";
            case "中" -> "comment";
            case "低" -> "info";
            default -> "other";
        };
        return createFont(priority, color);
    }

    public static String createTaskStatus(String status) {
        String color = switch (status) {
            case "finished" -> "warning";
            case "中" -> "comment";
            case "低" -> "info";
            default -> "other";
        };
        return createFont(status, color);
    }

    // 生成段落
    public static String createParagraph(String text) {
        return text.trim() + "\n\n";
    }

    // 生成无序列表
    public static String createUnorderedList(String[] items) {
        StringBuilder sb = new StringBuilder();
        for (String item : items) {
            sb.append("- ").append(item).append("\n");
        }
        sb.append("\n");
        return sb.toString();
    }

    public static String createQuoteUnorderedList(List<String> items) {
        StringBuilder sb = new StringBuilder();
        for (String item : items) {
            sb.append(createQuote("- " + item));
        }
        return sb.toString();
    }

    // 生成有序列表
    public static String createOrderedList(String[] items) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= items.length; i++) {
            sb.append(i).append(". ").append(items[i - 1]).append("\n");
        }
        sb.append("\n");
        return sb.toString();
    }

    public static String createOrderedList(List<String> items) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= items.size(); i++) {
            if (StringUtils.isNotEmpty(items.get(i - 1))) {
                sb.append(i).append(". ").append(items.get(i - 1)).append("\n");
            }
        }
        sb.append("\n");
        return sb.toString();
    }

    public static String createQuoteOrderedList(List<String> items) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= items.size(); i++) {
            sb.append(createQuote(i + ". " + items.get(i - 1)));
        }
        return sb.toString();
    }

    // 生成代码块
    public static String createCodeBlock(String language, String code) {
        StringBuilder sb = new StringBuilder();
        sb.append("```").append(language).append("\n");
        sb.append(code).append("\n");
        sb.append("```\n");
        return sb.toString();
    }

    // 生成链接
    public static String createLink(String text, String url) {
        return "[" + text + "](" + url + ")";
    }

    // 生成图片
    public static String createImage(String altText, String url) {
        return "![" + altText + "](" + url + ")";
    }

    // 生成 label 和 value
    public static String createMap(Map<String, String> body) {
        if (body == null || body.isEmpty()) return "";
        return body.entrySet().stream()
            .map(entry -> "> **" + entry.getKey() + "**: " + entry.getValue())
            .collect(Collectors.joining("\n"));
    }

    // 生成表格
    public static String createTable(String[][] headersAndRows) {
        StringBuilder sb = new StringBuilder();
        sb.append("| ");
        // 添加表头
        for (String header : headersAndRows[0]) {
            sb.append(header).append(" | ");
        }
        sb.append("\n");

        // 添加分隔线
        sb.append("| ");
        sb.append(":--: | ".repeat(headersAndRows[0].length));
        sb.append("\n");

        // 添加表格内容
        for (int i = 1; i < headersAndRows.length; i++) {
            sb.append("| ");
            for (String cell : headersAndRows[i]) {
                sb.append(cell).append(" | ");
            }
            sb.append("\n");
        }
        sb.append("\n");
        return sb.toString();
    }

    public static void main(String[] args) {
        // 示例使用
        String markdown = MarkdownUtil.createHeaderN(1, "示例 Markdown") +
            MarkdownUtil.createParagraph("这是一个示例段落。") +
            MarkdownUtil.createUnorderedList(new String[]{"项1", "项2", "项3"}) +
            MarkdownUtil.createOrderedList(new String[]{"第一项", "第二项", "第三项"}) +
            MarkdownUtil.createCodeBlock("java", "public class HelloWorld { public static void main(String[] args) { System.out.println(\"Hello, World!\"); } }") +
            MarkdownUtil.createLink("点击这里", "https://www.example.com") +
            MarkdownUtil.createImage("示例图片", "https://www.example.com/image.jpg") +
            MarkdownUtil.createTable(new String[][]{
                {"表头1", "表头2"},
                {"数据1", "数据2"},
                {"数据3", "数据4"}
            });

        System.out.println(markdown);
    }
}
